using System;
using System.Windows.Media;
using System.Windows.Threading;
using SmartBoard.BackGround.BoardElement;
using SmartBoard.BackGround.Data;
using SmartBoard.Core.Data;

namespace SmartBoard.Background.BoardElement
{
    internal class BackgroundDataBroker:IBoardElementDataBroker
    {
        private readonly BackGroundBoardElement _element;

        public BackgroundDataBroker(BackGroundBoardElement element)
        {
            _element = element;
        }

        #region Implementation of IBoardElementDataBroker

        public byte GetElementDataVersion()
        {
            return (byte)DataVersion.V10;
        }

        public IBoardElementData GetData()
        {
            var brush = (SolidColorBrush) _element.Background;
            return new V10BackgroundData()
                       {
                           A = brush.Color.A,
                           R = brush.Color.R,
                           G = brush.Color.G,
                           B = brush.Color.B
                       };
        }

        public void SetData(IBoardElementData data)
        {
            if (data == null)
                return;
            var backgroundData = (V10BackgroundData) data;
            _element.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                                                {
                                                    _element.Background = new SolidColorBrush(Color.FromArgb(
                                                                                                  backgroundData.A,
                                                                                                  backgroundData.R,
                                                                                                  backgroundData.G,
                                                                                                  backgroundData.B));
                                                }));
        }

        #endregion
    }
}